package com.zzmcc.tower;

import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.jms.model.Tower;
import util.ChristStringUtil;
import util.ChristDateUtils;
import util.CommonUtil;


import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Tower;

public class TowerController extends BaseController {
    private static final Logger log =  Logger.getLogger(TowerController.class);

	public void index() {
        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();

        String name = getPara("tower.name");
        String owner =getPara("tower.owner");
        String status =  getPara("tower.status");

        try {
             name  = URLDecoder.decode(getPara(1), "UTF-8");
             owner  = URLDecoder.decode(getPara(2), "UTF-8");
             status  = URLDecoder.decode(getPara(3), "UTF-8");
        } catch (Exception e) {

        }
        if(ChristStringUtil.isNotEmpty(name)){
            whee.append(" and name like ?");
            param.add("%" + name + "%");
        }

        if(ChristStringUtil.isNotEmpty(owner)){
            whee.append(" and owner = ?");
            param.add( owner );
        }
        if(ChristStringUtil.isNotEmpty(status)){

            whee.append(" and status = ?");
            param.add( status );
        }
       // log.warn("name:"+name);
        Page<Tower> towerPage = Tower.dao.paginate(
                getParaToInt(0, 1), 10,
                "select * ",
                " from " + TABLE_Tower +" where 1=1  "+ whee.toString(),
                param.toArray());

        setAttr("towerPage",towerPage);
        StringBuffer urlParas = new StringBuffer();
        urlParas.append("-");
        if(ChristStringUtil.isNotEmpty(name)){
            urlParas.append(name);
        }

        urlParas.append("-");
        if(ChristStringUtil.isNotEmpty(owner)){
            urlParas.append(owner);
        }
        urlParas.append("-");
        if(ChristStringUtil.isNotEmpty(status)){
            urlParas.append(status);
        }

        setAttr("urlParas",urlParas);

        render("index.html");
	}

    public void view(){
        setAttr("tower",Tower.dao.findById(getParaToInt()));
        render("view.html");
    }

    public void update(){
        Tower s = getModel(Tower.class, "tower");
        s.update();
        setAttr("operateStr", "操作成功!");
        render("manage.html");
    }

    public void add(){

        render("add.html");
    }

    public void save(){
        Tower s = getModel(Tower.class, "tower");
        s.set("addr_code","盘点手工录入"+ CommonUtil.getRandomNumber(4));
        s.save();
        setAttr("operateStr", "操作成功!");
        render("manage.html");
    }

    public void report(){
        StringBuffer sb= new StringBuffer();
        String sql ="SELECT  owner,count(*) cnt,\n" +
                "  sum(case when status='是' then 1 else 0 end) t_cnt,\n" +
                "  concat(round(sum(case when status='是' then 1 else 0 end)/count(*)*100,2),'%') as t_rate,\n" +
                "  sum(case when status='是' and to_days(modifyDate)=to_days(now()) then 1 else 0 end) d_cnt\n" +
                "from jms_tower\n" +
                "GROUP BY  owner ORDER BY owner ";
        List<Record> recordList = Db.find(sql);
        int i=0;
        sb.append("漳州公司"+ChristDateUtils.toString(ChristDateUtils.getCurrentDate())+"清查日报:");
        for(Record x:recordList){
            i+=1;
            sb.append(x.getStr("owner")+"完成"+x.get("d_cnt")+",累计"+x.get("t_cnt")+",占比"+x.getStr("t_rate"));
            if (i<recordList.size()){
                sb.append("；");
            }

        }
        renderText(sb+"。");
    }


}
